local function AT2_IsSecure()
	AT2_Print(debugstack(2, 1, 1),"debug","AT2_IsSecure")
	if (debugstack(2,1,1):find("Interface\\AddOns\\AutoTalker2\\")) then
		return true
	else
		return false
	end
end

local AT2_Modules={} 

function AT2_ModulesT()
	if (AT2_IsSecure()) then
		return AT2_Modules
	else
		return nil
	end
end

local AT2_FrameForModuleUpdate=CreateFrame("Frame")
local frameNum=0
AT2_FrameForModuleUpdate:SetScript("OnUpdate",AT2_Modules_OnUpdate)
local AT2_TrustedAddonList={"AutoTalker2"}
local AT2_CoreModules={}
AT2_CoreModules["Misc"]="required" -- old list, will be updated with rewritten equivalent
AT2_CoreModules["Savehandling"]="required"
AT2_CoreModules["Patternmatching"]="required"
AT2_CoreModules["Errorchecking"]="required"
AT2_CoreModules["Eventhandling"]="required"
AT2_CoreModules["Savedmessages"]="required"
AT2_CoreModules["Send"]="required"
AT2_CoreModules["Timer"]="required"
AT2_CoreModules["UI-lua"]="required"
AT2_CoreModules["UI-xml"]="required"
AT2_CoreModules["SavedPanel"]="required" -- ^ old list
AT2_CoreModules["lowdebug"]="optional"
AT2_CoreModules["dev"]="optional"

--[[ --generic modules save structure
AT2_Save["Modules"] = {
	["ModnameABCDEFGH"]={
		["Enabled"]=true,
		["Settings"]={},
		},
	}
]]

local function AT2_Modules_OnUpdate()
	frameNum=(frameNum>15 and 1) or frameNum+1
	if (frameNum%2==0) then
		for name,mod in pairs(AT2_Modules) do
			if mod["OnUpdate"] then
				if (mod["OnUpdateImportant"] or AT2_CoreModules[name]) and ((InCombatLockdown() and frameNum%4==0) or not(InCombatLockdown())) then
					mod["OnUpdate"]()
				elseif mod["OnUpdateLowPriority"] and frameNum%16==0 then
					mod["OnUpdate"]()
				elseif ((InCombatLockdown() and frameNum%8==0) or (not(InCombatLockdown())) and frameNum%4==0) then
					mod["OnUpdate"]()
				end
			end
		end
	end
end

function AT2_Modules_RegisterForSend(module)
--NYI
	if (AT2_IsSecure()) then
		--local rateLimitFunc = module["RegisterForSend"](remain)
	end
end

local function AT2_ValidateModule(module)
--NYI
--local problemList={}
--if (not(tContains(AT2_ModAddonList,AddonName)) or tContains(AT2_TrustedAddonList)) then
--if module["data"]["minVersion"] <= AT2_ReleaseVersion then
--if no api functions that break at module["data"]["writtenForVersion"] then

--if #problemList==0 then 
--	return true
--else
--	return false, problemList
--end
end


function AT2_InsertModule(module)
	if (AT2_ValidateModule(module)) then
		local ID = module["name"]..strchar(random(65,90))..strchar(random(65,90))..strchar(random(65,90))..strchar(random(65,90))..strchar(random(65,90))..strchar(random(65,90))..strchar(random(65,90))..strchar(random(65,90)) -- add random string, so external malicious modules cannot overwrite existing ones
		AT2_Modules[ID]=module["data"]
		return true
	else
		return false, AT2_ValidateModule(module)
	end
end

-- -- New module system above -- --
-- -- -- -- -- -- -- -- -- -- -- --

function AT2_StartModules()
AT2_FailedModules = {}
AT2_AddModule("Misc")
AT2_AddModule("Savehandling")
AT2_AddModule("Patternmatching")
AT2_AddModule("Errorchecking")
AT2_AddModule("Eventhandling")
AT2_AddModule("Savedmessages")
AT2_AddModule("Send")
AT2_AddModule("Timer")
AT2_AddModule("UI-lua")
AT2_AddModule("UI-xml")
AT2_AddModule("SavedPanel")	
if (AT2_OneOrMoreModulesFailedToLoad==1) then
AT2_ModuleErrorGenString = ""
for i=1,#AT2_FailedModules,1 do AT2_ModuleErrorGenString=AT2_ModuleErrorGenString.." "..AT2_FailedModules[i] end
AT2_Print("Egy vagy több modul betöltése sikertelen volt! Kérlek töltsd le újra az addont és ellenőrizd, hogy az összes fájl a helyén van-e! Ha ezek után is kapod a hibát, kérlek jelentsd a hibát a [insert webcím here] oldalon. (Hibás modulok:"..AT2_ModuleErrorGenString..")","error")
else
AT2_AllModulesLoaded=1
AT2_Print("Addon sikeresen betöltve. ("..GetAddOnMetadata("AutoTalker2","Version").."."..GetAddOnMetadata("AutoTalker2","X-Revision")..")")
end
end

function AT2_AddModule(module)
AT2_Print("Modul \""..module.."\" betötése.. "..select(2, AT2_CheckModule(module)).."","debug","AddModule")
if (select(1, AT2_CheckModule(module)) == 0) then
tinsert(AT2_FailedModules, module)
end

end

function AT2_CheckModule(module)
if not(tContains(AT2_ModuleList,module)) then
AT2_OneOrMoreModulesFailedToLoad=1
return 0, "\124cffFF0000\HIBA!\124r"
else
return 1, "\124cff00FF00\OK!\124r"
end
end

AT2_StartModules() -- Ezt majd áttenni egy Load()-ba, majd áttenni az xml-be.